package exercicios.servlet.ex01;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/servlet/ex01")
public class ExibeNomeTelefoneServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{
		processar(req, res);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{
		processar(req, res);
	}

	private void processar(HttpServletRequest req, HttpServletResponse res) throws IOException {
		String nome = req.getParameter("nome");
		String telefone = req.getParameter("telefone");
		PrintWriter out = res.getWriter();
		res.setContentType("text/html");
		String telefoneFmt = String.format("(%s) %s-%s",
				telefone.substring(0,2),
				telefone.substring(2,6),
				telefone.substring(6,10));
		out.println("<html><head><title>Servlet Exercicio 1</title></head>");
		out.println("<body>");
		out.println("<p>Nome de usu&aacute;rio: "+ nome +" Telefone: "+telefoneFmt+"</p>");
		out.println("</body></html>");
	}
}
